#/usr/bin/perl -w
use strict;

sub getNumLines {
  my ($in) = @_;
  system("wc -l $in > temp.txt");
  open(IN, "< temp.txt") or die;
  my $num = <IN>;
  close(IN);
  my @chunks = split(' ',$num);
  $num = $chunks[0];
  $num =~ s/\s+$//; $num =~ s/^\s+//;
  return $num;
};

sub printBar {
  my ($num) = @_;
  my $mult=1;
  while($num/$mult > 80) {
    $mult++;
  }
  $num = int($num/$mult+0.5);
  print STDERR "|";
  for(my $i=1;$i<=$num-2;$i++) {
    if($i==int(($num-2)/2+0.501)) { print STDERR "\!"; }
    elsif($i==int(($num-2)/4+0.501)) { print STDERR "\#"; }
    elsif($i==int(3*($num-2)/4+0.501)) { print STDERR "\#"; }
    elsif($i%5==0) { print STDERR "+"; }
    else { print STDERR "-"; }
    if($i%100==0) { print STDERR "\n"; }
  }
  print STDERR "|\n";
  return $mult;
};

sub clean {
  my ($str) = @_;
  $str =~ s/\s+$//; $str =~ s/^\s+//;
  return $str;
};

sub list2vec {
  my (@list) = @_;
  my $vec;
  foreach my $item (@list) {
    $vec .= $item.",";
  }
  return $vec;
};

sub vec2list {
  my ($vec) = @_;
  return split(',',$vec);
};

sub vec2pdb {
#HETATM91176  O   HOH   496
  my ($vec,$count,$num,$resnum) = @_;
  my ($x,$y,$z,$r) = vec2list($vec);
  my $line = "HETATM";
  $line .= fixnum50($count);
  $line .= "  O   HOH  ";
  $line .= fixnum40($resnum);
  $line .= "    ";
  $line .= fixnum43($x+0.012);
  $line .= fixnum43($y+0.034);
  $line .= fixnum43($z+0.056);
  $line .= "  1.00";
  if($num) {
    $line .= fixnum32($num);
  } else {
    $line .= fixnum32(0.0);
  }
  $line .= "\n";
  return $line;
};

return 1;
